import db.sqlite
import dao.entity
import time

fn main() {
	test_create_table()
	test_insert()
	test_select()
	test_update()
	//test_select_single()
    //test_select_by_word()
    //test_select_with_optional()
}

//测试创建表
fn test_create_table() {
	mut db := sqlite.connect('memory_palace.db') or { panic(err) }
	db.synchronization_mode(sqlite.SyncMode.off) or { panic(err) }
	db.journal_mode(sqlite.JournalMode.memory) or { panic(err) }

	defer { db.close() or {} }

	sql db {
		create table entity.WordMemoryEncoding
	} or { panic(err) }
}

//测试插入记录
fn test_insert() {
	mut db := sqlite.connect('memory_palace.db') or { panic(err) }
	mut encoding := entity.defautt_create(
		encoding:   'python is good'
		word:       'python'
		//created_at: time.now().format_ss()
	)
	sql db {
		insert encoding into entity.WordMemoryEncoding
	} or { panic(err) }
}

//测试更新记录
fn test_update() {
	mut db := sqlite.connect('memory_palace.db') or { panic(err) }

	id_to_update := 1
	sql db {
		update entity.WordMemoryEncoding set is_deleted = true, deleted_at = time.now().format_ss()
		where id == id_to_update
	} or { panic(err) }
}

//测试查询记录
fn test_select() {
	mut db := sqlite.connect('memory_palace.db') or { panic(err) }

	encodings := sql db {
		select from entity.WordMemoryEncoding
	} or { panic(err) }

	println('查询到 ${encodings.len} 条记录:')
	for i, encoding in encodings {
		id_value := encoding.id or { -1 } // 解包可选 ID
		println('[${i}] ID: ${id_value}, Word: "${encoding.word}", Created: ${encoding.created_at}')
	}
}

//查询单条记录
fn test_select_single() {
	mut db := sqlite.connect('memory_palace.db') or { panic(err) }
	defer { db.close() or {} }

	target_id := 1

	encoding := sql db {
		select from entity.WordMemoryEncoding where id == target_id limit 1
	} or { panic(err) }

	if encoding.len > 0 {
		println('找到记录: ID: ${encoding[0].id}, Word: "${encoding[0].word}"')
	} else {
		println('未找到 ID 为 ${target_id} 的记录')
	}
}
//带条件查询
fn test_select_by_word() {
    mut db := sqlite.connect('memory_palace.db') or { panic(err) }
    defer { db.close() or {} }
    
    word_to_find := "rust"
    
    encodings := sql db {
        select from entity.WordMemoryEncoding where word == word_to_find
    } or { panic(err) }
    
    println('找到 ${encodings.len} 条包含 "$word_to_find" 的记录')
    for encoding in encodings {
        println('ID: ${encoding.id}, Encoding: "${encoding.encoding}"')
    }
}
//处理可选字段
fn test_select_with_optional() {
    mut db := sqlite.connect('memory_palace.db') or { panic(err) }
    defer { db.close() or {} }
    
    encodings := sql db {
        select from entity.WordMemoryEncoding
    } or { panic(err) }
    
    println('查询到 ${encodings.len} 条记录:')
    for encoding in encodings {
        id_value := encoding.id or { -1 } //使用or解包Option类型，若为none则返回-1
        deleted_at_value := encoding.deleted_at or { 'NULL' }
        
        println('ID: $id_value, Word: "${encoding.word}", Encoding: "${encoding.encoding}", Deleted: ${encoding.is_deleted}, DeletedAt: $deleted_at_value')
    }
}